import util from '../../util';
import LogicInstance from '../../../service2/scenario/instance/LogicInstance';
import parentInstance from '../parentInstance';
export default class logic extends parentInstance{
    constructor() {
        super();
        this.util = util;
        this.typeList = ['logicDictionary', 'logicTree', 'logicQueue', 'logicSingleton'];
        this.record = {};
        this.info = null;
        this.pointer = null;
        this.modelMap = null;
        this.hier = null;
        this.others = null;
        /** modelMap格式
         * {
            table: tableName,
            featureList: new Set(),
            valueList: new Set(),
            fieldList: new Set(),
            groupList: new Set(),
            fieldMap: {
            fieldname；fieldtype
            },
          }
         */
    }
    static create (record) {
        let object = new logic();
        if (!!record) {
            object.setRecord(record);
        };
        return object;
    }
    setRecord (record) {
        // if(!!record.id&&!!record.type&&!!record.className){
        //
        // }else{
        //     this.record={};
        //     this._setPointer(false);
        // }
        this.record = record;
        this._setPointer();
        this._setHier();
        this._setOthers();
    }
    _setPointer (empty = true) {
        if (!empty) {
            this.info = null;
            this.pointer = null;
            this.modelMap = null;
            this.hier = null;
            this.others = null;
            return;
        }
        this.pointer = {
            id: this.record.id,
            type: this.record.type,
            version: this.record.version,
            className: this.record.className
        };
        this.info = {
            id: this.record.id,
            name: this.record.name,
            type: this.record.type,
            className: this.record.className,
            version: this.record.version,
            published: this.record.published
        };
        // this.modelMap=this._getModelMap();
    }
    _setHier (hier) {
        if (!hier) {
            this.hier = this.record.hier;
            return;
        }
        this.hier = this.record.hier = hier;
    }
    _setOthers (others) {
        if (!others) {
            this.others = this.record.others;
            return;
        }
        this.others = this.record.others = others;
    }
    _getModelMap () {
        return util.modelState.map.get([this.pointer.type, this.pointer.className].join(','));
    }
    /*******************************************************************************
     * 对象属性编辑功能
     * ******************************************************************************/
    /***
     * 新增
     * @param record
     * @returns {Promise<boolean|*>}
     */
    /* async add(record){
        if(!record||!record.type||!record.className||record.type==''||record.className==''){
            return false;
        }
        let newRecord=Object.assign(logic.tpl(),record);
        let result = await util.api.LogicInstance.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    } */
    /**
     * 删除
     * @returns {Promise<boolean|*>}
     */
    /* async delete(){
        if(!!this.pointer){
            let result = await util.api.LogicInstance.delete(this.pointer.id).then(flag=>{
                if(flag){
                    this.setRecord([]);
                    return true;
                }else{
                    return false;
                }
            }).catch(err=>{
                return false;
            });
            return result;
        }else{
            return false;
        }

    } */
    /**
     * 更新
     * @returns {Promise<*>}
     */
    async update () {
        let updateRec = {
            id: this.record.id,
            type: this.record.type,
            className: this.record.className,
            name: this.record.name,
            version: this.record.version,
            published: this.record.published,
            features: this.record.features,
            values: this.record.values,
            hier: this.record.hier,
            web: this.record.web,
            others: this.record.others,
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: this.record.comments
        }
        let result = await util.api.LogicInstance.update(updateRec).then(flag => {
            if (flag) {
                return true;
            } else {
                return false;
            }
        }).catch(err => {
            return false;
        });
        return result;
    }
    /**
     * 查询
     * @param param
     * @returns {Promise<*>}
     */
    async query (filter) {
        let result = await util.api.LogicInstance.query({ condition: filter }).then(data => {
            if (!!data) {
                this.setRecord(data[0]);
                return true;
            } else {
                return false;
            }
        }).catch(err => {
            return false;
        });
        return result;
    }

    /**
     * 运行时注册,更新hier中某项
     * @param pointer 注册指针
     * @param hier  转换成map,如:{"basic":[]}
     * @returns {*}
     */
    async runRegister (pointer, hier) {
        let result = await util.api.LogicInstance.runRegister(pointer, hier).then(data => {
            if (!!data) {
                this._setHier(data);
                return true;
            } else {
                return false;
            }
        }).catch(err => {
            return false;
        });
        return result;
    }
    /**
     * 运行时容器添加注册
     * @param pointer 注册指针
     * @param hier  转换成map,如:{"basic":[]}
     * @returns {*}
     */
    async runAddRegister (pointer, hier) {
        let result = await util.api.LogicInstance.runAddRegister(pointer, hier).then(data => {
            if (!!data) {
                this._setHier(data);
                return true;
            } else {
                return false;
            }
        }).catch(err => {
            return false;
        });
        return result;
    }
    /**
     * 运运时容器移除注册
     * @param pointer 取消订阅指针
     * @param names 字符数组，如 [a,b]
     * @returns {*}
     */
    async runRemoveRegister (pointer, names) {
        let result = await util.api.LogicInstance.runRemoveRegister(pointer, names).then(data => {
            if (!!data) {
                this._setHier(data);
                return true;
            } else {
                return false;
            }
        }).catch(err => {
            return false;
        });
        return result;
    }
    async runAllAddRegister(pointer,hier){
        let result = await util.api.LogicInstance.runAllAddRegister(pointer,hier).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 通过其他模型中的others获取信息
     * @param {*} id
     * @returns
     */
    getOtherFatherById (id) {
        if (!this.hier || this.hier.length === 0) {
            return {}
        }
        this._setPointer();
        let otherInfo = {};
        // 找到节点
        let index = this.hier[0].basic.findIndex(d => String(d.id) === String(id))
        let parentId = '';
        // 找到父节点
        if (index !== -1) {
            parentId = this.hier[0].basic[index].parentId
        }
        let parentIndex = this.hier[0].basic.findIndex(d => String(d.id) === String(parentId))
        if (parentIndex !== -1) {
            otherInfo = this.hier[0].basic[parentIndex].values
        }
        return otherInfo;
    }
    getOtherById (id) {
        if (!this.hier || this.hier.length === 0) {
            return {}
        }
        this._setPointer();
        let otherInfo = {};
        // 找到节点
        let index = this.hier[0].basic.findIndex(d => String(d.id) === String(id))
        if (index !== -1) {
            otherInfo = this.hier[0].basic[index].values
        }
        return otherInfo;
    }
    //获取当前节点所有子项,返回数组
    getChildrenById (id) {
        if (!this.hier || this.hier.length === 0) {
            return []
        }
        let children = this.hier[0].basic.filter(d => String(d.parentId) === String(id))
        return children;
    }

    /**
     * hier的basic是否有值
     */
    isValidHier () {
        if (!this.hier || this.hier.length === 0 || !this.hier[0].basic || this.hier[0].basic.length === 0) {
            return false;
        } else {
            return true
        }
    }
    /**
     * others的basic是否有值
     */
    isValidOthers () {
        if (!this.others || !this.others.basic || this.others.basic.length === 0) {
            return false;
        } else {
            return true
        }
    }

    /**
     * 新增模板
     * @returns {{comments: string, web: {}, values: {}, name: string, className: string, published: boolean, type: string, version: number, hier: *[], others: {}}}
     */
    static tpl () {
        return {
            // id: "",
            name: '',
            type: '',
            className: '',
            version: '1.0',
            published: true,
            values: {},
            hier: [],
            web: {},
            others: {},
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: ''
        }
    }
}
